<script setup lang="ts">
import { ref } from 'vue'
import KnowledgeCard from './KnowledgeCard.vue'
import type { KnowledgeType, KnowledgeList, KnowledgeParams } from '@/types/consult'
import { getKnowledgePage } from '@/services/consult'

// 需要父组件传入type才知道请求什么类型的文章
const props = defineProps<{
  type: KnowledgeType
}>()
// 定义查询参数
const params = ref<KnowledgeParams>({
  type: props.type,
  current: 1,
  pageSize: 10
})

const list = ref<KnowledgeList>([])
const loading = ref(false)
const finished = ref(false)
// 加载数据的函数：1.页面打开后默认执行一次 2.每次用户滚动到底部会再次执行
const onLoad = async () => {
  // 加载数据
  const res = await getKnowledgePage(params.value)
  // 把当前页数据追加到数组中去
  list.value.push(...res.data.rows)

  // 判断是否加载完毕
  if (params.value.current >= res.data.pageTotal) {
    // 加载完毕
    finished.value = true
  } else {
    // 没有加载完毕就继续加载
    params.value.current++
  }
  loading.value = false
}
</script>

<template>
  <div class="knowledge-list">
    <van-list
      v-model:loading="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <knowledge-card v-for="item in list" :key="item.id" :item="item"></knowledge-card>
    </van-list>
  </div>
</template>

<style lang="scss" scoped>
.knowledge-list {
  padding: 0 15px;
}
</style>
